package ai4games_tests.guides;

import game2dai.World;
import game2dai.entities.BaseEntity;
import game2dai.entities.Vehicle;
import game2dai.entityshapes.ps.Hints;
import game2dai.entityshapes.ps.ImageBank;
import game2dai.entityshapes.ps.PicturePS;
import game2dai.maths.Vector2D;
import game2dai.utils.StopWatch;
import processing.core.PApplet;
import processing.core.PImage;

public class Tank_02 extends PApplet {

	// AdvancedVehicle_01
	World world;
	StopWatch sw;
	Tank tank;
	TankPic view;
	Vector2D target = new Vector2D();

	public void setup() {
		size(600, 320);
		world = new World(width, height);
		sw = new StopWatch();
		// Create the mover
		tank = new Tank(new Vector2D(width/2, height/2), // position
				40, // collision radius
				new Vector2D(0, 0), // velocity
				50, // maximum speed
				new Vector2D(1, 0), // heading
				10, // mass
				1.5f, // turning rate
				1000 // max force
		); 
		// What does this mover look like
		view = new TankPic(this, "ai4g/tanks/turrets32_0.png", "ai4g/tanks/tanks_base32_0.png", 8, 1);    
		view.showHints(Hints.HINT_COLLISION | Hints.HINT_HEADING | Hints.HINT_VELOCITY);
		tank.renderer(view);
		// Finally we want to add this to our game domain
		world.add(tank);
		sw.reset();
	}

	public void draw() {
		double elapsedTime = sw.getElapsedTime();
		target.set(mouseX, mouseY);
		tank.AP().arriveOn(target);

		world.update(elapsedTime);
		background(218, 140, 54);
		world.draw();
	}

	public void mouseMoved(){
		target.set(mouseX, mouseY);
		tank.aimTurretAtPos(target);
	}



	public class Tank extends Vehicle {

		public int frameNo = 0;
		public float turretAngle;
		public float turretTargetAngle;
		public float lastTime = 0;
		public Vector2D targetPos = null;

		// This can be copied from the Vehicle class constructor
		public Tank(Vector2D position, double radius, Vector2D velocity,
				double max_speed, Vector2D heading, double mass,
				double max_turn_rate, double max_force) {
			super(position, radius, velocity, max_speed, heading, mass, max_turn_rate,
					max_force);
		}

		public void aimTurretAtPos(Vector2D turretTarget){
			if(turretTarget == null)
				turretTargetAngle = 0;
			else {
				targetPos = Vector2D.sub(turretTarget, pos).normalize();
				turretTargetAngle = (float) targetPos.angleBetween(heading);
			}
		}
		
		public void update(double elapsedTime, World world){
			super.update(elapsedTime, world);
			lastTime += (float)elapsedTime;

			float speed = (float) speed();
			if (speed > 1) {
				float interval = map(speed, 0, (float)maxSpeed, 0.5f, 0.05f);
				if(lastTime > interval){
					lastTime -= interval;
					frameNo++;
				}
			}
			else {
				lastTime = 0;
			}
			// Needs tweaking
			if(turretAngle != turretTargetAngle){
				turretAngle += (turretTargetAngle - turretAngle) *  (float) elapsedTime * 3;
			}
		}
	}

	public class TankPic extends PicturePS {

		PImage[] base;
		PImage[] turret;


		public TankPic(PApplet app, String tankTurret, String tankBase, int cols, int rows){
			super(app);
			turret = ImageBank.getImage(app, tankTurret);
			base = ImageBank.getImage(app, tankBase, cols, rows);
		}

		public void draw(BaseEntity owner, float posX, float posY, float velX,
				float velY, float headX, float headY) {

			Tank tank = (Tank) owner;

			// Draw and hints that are specified and relevant
			if(hints != 0){
				Hints.hintFlags = hints;
				Hints.draw(app, owner, velX, velY, headX, headY);
			}
			// Determine the angle the tank is heading
			float angle = PApplet.atan2(headY, headX);

			// Prepare to draw the entity		
			app.pushStyle();
			app.imageMode(PApplet.CENTER);
			app.pushMatrix();
			app.translate(posX, posY);
			//			app.pushMatrix();
			app.rotate(angle);

			// Draw the entity		
			app.image(base[tank.frameNo % 8],0,0);
			//			popMatrix();
			app.rotate(tank.turretAngle);
			app.image(turret[0], 0, 0);
			// Finished drawing
			app.popMatrix();
			app.popStyle();			

		}
	}
}
